
const service = require('./../service');
const http = require('./../commons/http');
const config = require('./../config');
let logger = require('./../commons/logger');

exports = module.exports = {
  sendEnsureCode: async (ctx, next) => {
    let mobile = ctx.params&&ctx.params.mobile || '';

    logger.info('sendEnsureCode', mobile);
    try {
      let ret = await service.sendEnsureCode(ctx, mobile);
      if (ret.code !== 10000) {
        throw new BSPError(ret);
      } else {
        logger.debug('sendEnsureCode', ret.data);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '获取验证码成功' };
      }
    } catch (e) {
      logger.error('sendEnsureCode Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  ensureAccount: async (ctx, next) => {
    let params = ctx.request.body;
    logger.info('ensureAccount', params);
    try {
      let ret = await service.ensureAccount(ctx, params);
      if (ret.code !== 10000) {
        throw new BSPError(ret);
      } else {
        ctx.session.person = ret;
        
        let result = ret.data;
        logger.debug('ensureAccount', result);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '电话号码绑定成功', data: result };
      }
    } catch (e) {
      logger.error('ensureAccount Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  sign: async (ctx, next) => {
    let params = ctx.request.body;
    logger.info('sign', params);
    try {
      let ret = await service.sign(ctx, params);
      if (ret.code !== 10000) {
        throw new BSPError(ret);
      } else {
        let result = ret.data;
        logger.debug('sign', result);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '签到成功', data: result };
      }
    } catch (e) {
      logger.error('sign Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  getSecurity: async (ctx, next) => {
    try {
      logger.info('getSecurity query:', ctx.query);
      let ret = await http.get(ctx, config.restapi.backend + '/security', { params: ctx.query });
      if (ret.code === 10000) {
        ctx.body = ret.data.echostr;
      } else {
        throw new Error('服务器发生错误');
      }
    } catch (e) {
      ctx.body = {
        code: 10001,
        msg: '服务器发生错误',
        data: null
      }
      console.log(e)
    }
  },
  postSecurity: async (ctx, next) => {
    try {
      if (ctx.is('text/xml')) {

        let xmlWorker = (ctx) => {
          return new Promise((resolve, reject) => {
            let buf = '';
            ctx.req.on('data', (chunk) => {
              buf += chunk;
            })
            ctx.req.on('end', () => {
              resolve(buf);
            })
            ctx.req.on('error', (error) => {
              reject(error);
            })
          })
        }

        let ret = await xmlWorker(ctx);
        ret = await http.post(ctx, config.restapi.backend + '/security', { 'repXML': ret });
        logger.info('a:', ret);
        ctx.type = 'text/xml';
        ctx.body = ret.data.respXML || '';
      }
    } catch (e) {
      ctx.body = 'failed'
      console.log(e)
    }
  }
}